define(function (require) {
var zsApp = require('app');  
zsApp.controller('classificationTreeCtrl', ['$rootScope', '$scope', '$http', '$state', 'cache', function($rootScope, $scope, $http, $state, cache) {
	
	loadTree();
	function loadTree() {
		$http({
			url		: $scope.zsUrl+'digitalhome-boss/product/proCategors/getParentInfos',
			method  : 'GET',
		})
		.success(function(res) {

			var tree = [];
			for(var i=0;i<res.result.length;i++) {
				if( '0' === res.result[i].parentId) {
					tree.push(res.result[i])
				}
			};
			var fn = function(item,data) {
				for(var i = 0 ; i<data.length;i++) {
					var each = data[i];
					if(!item.children){
						item.children = [];
					}
					if(each.parentId == item.productCategoryId ){
						item.children.push(each);
						fn(each,data)
					}
				}
			};
			for(var i = 0;i<tree.length;i++) {
				fn(tree[i],res.result)
			};
			
			$scope.treeOptions = {
			    nodeChildren		: 'children',
			    dirSelectable		: true,
			    injectClasses		: {
			        ul 				: 'a1',
			        li 				: 'a2',
			        liSelected		: 'a7',
			        iExpanded		: 'a3',
			        iCollapsed		: 'a4',
			        iLeaf			: 'a5',
			        label 			: 'a6',
			        labelSelected	: 'a8'
			    }
			};

			$scope.dataForTheTree = tree;

			$scope.showSelected = function(node) { loadTree();
				cache.put('productName', node.productName);
				cache.put('productCategoryId', node.productCategoryId);
				cache.put('parentId', node.parentId);

				var treeReqVo = {
					id 	: cache.get('productCategoryId'),
				};
				$http({
					url		: $scope.zsUrl+'digitalhome-boss/product/proCategors/getSublevelInfo',
					method	: 'POST',
					data   : treeReqVo,
				}).success(function(res) {
					// console.log(res);
				}).error(function(err) {
					// console.log(err);
				});
			};
		});
	};

	// show magami tree
	$scope.showMogami = function () {
		cache.put('productCategoryId', 0);
		cache.put('productName', '最上级');
		$state.go('goods.classification', {}, { reload: true });
	}
	}]);
});